package grape.workflow.rest;

import grape.common.rest.common.GlobalInterceptor;
import grape.common.rest.common.SwaggerConfig;
import grape.workflow.rest.flowable.ui.FlowableUserInterceptor;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.ServletContext;
import java.util.Arrays;

/**
 * 将配置单独提出来，以实现被依赖时自动配置
 * Created by yangwei
 * Created at 2019/7/25 9:04
 */
/* 配置 ui modeler */
/*@Import({
        ApplicationConfiguration.class,
        DispatcherServletConfiguration.class
})*/
@Configuration
@EnableSwagger2
public class WorkflowRestConfig implements WebMvcConfigurer, SwaggerConfig {
    @Autowired
    private ServletContext servletContext;
    @Value("${spring.application.name}")
    private String applicationName;
    @Bean
    public FlowableUserInterceptor flowableUserInterceptor(){
        return new FlowableUserInterceptor();
    }

    /**
     * 应该在{@link GlobalInterceptor} 之后，但这里没有指定顺序，发现现在运行也是在其后，如果不是请指定顺序
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(flowableUserInterceptor()).addPathPatterns("/**");
    }
    @Bean
    public Docket createWorkflowRestApi() {
        return docket()
                .pathProvider(relativePathProviderForBase("/grape-workflow",applicationName,servletContext))
                .groupName("工作流引擎")
                .apiInfo(apiInfo("Workflow"))
                .select()
                //选择controller包
                .apis(basePackage("grape.workflow.rest"))
                .apis(withClassAnnotation(Api.class))
                // 内部调用接口不监控
                .paths(notPath("inner"))
                .paths(notPath("/oauth/token"))
                .build().globalOperationParameters(Arrays.asList(authorization()));
    }

}
